package com.woniu.microworkorderserver.businessmag.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.woniu.microcommonsserver.entity.InspectItemInfoEntity;
import com.woniu.microcommonsserver.entity.PollingTemplateInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microworkorderserver.businessmag.mapper.InspectItemMapper;
import com.woniu.microworkorderserver.businessmag.mapper.InspectTemplateMapper;
import com.woniu.microworkorderserver.businessmag.service.InspectTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InspectTemplateServiceImpl implements InspectTemplateService {

    @Resource
    private InspectTemplateMapper inspectTemplateMapper;
    @Resource
    private InspectItemMapper inspectItemMapper;

    @Override
    public Long save(PollingTemplateInfoEntity templateInfoEntity) {
        inspectTemplateMapper.save(templateInfoEntity);
        return templateInfoEntity.getId();
    }

    @Override
    public void saveInspectInfo(List<PollingTemplateInfoEntity> list1, List<InspectItemInfoEntity> list2) {
        PollingTemplateInfoEntity polling = new PollingTemplateInfoEntity();
        if(list1.size() > 0) {
            polling = list1.get(0);
            inspectTemplateMapper.save(polling);
            if(list2.size() > 0){
               for (InspectItemInfoEntity itemInfoEntity:list2){
                 itemInfoEntity.setFkInspectTemplateId(polling.getId());
                 inspectItemMapper.save(itemInfoEntity);
               }
            }
        }
    }

    @Override
    public PageInfo<PollingTemplateInfoEntity> findByPage(PageRequest page,PollingTemplateInfoEntity entity) {
        return PageHelper.startPage(page.getPageNum(),page.getLimit()).
                doSelectPageInfo(() -> inspectTemplateMapper.findByPage(page,entity));
    }

    @Override
    public void updateInspectInfo(List<PollingTemplateInfoEntity> list1, List<InspectItemInfoEntity> list2) {
        if(list1.size() > 0) {
            PollingTemplateInfoEntity polling = list1.get(0);
            inspectTemplateMapper.update(polling);
            if(list2.size() > 0){
                for (InspectItemInfoEntity itemInfoEntity:list2){
                    itemInfoEntity.setFkInspectTemplateId(polling.getId());
                    inspectItemMapper.update(itemInfoEntity);
                }
            }
        }else {
            System.out.println("删除失败");
        }
    }

    @Override
    public void deleteInspectInfo(long id) {
        inspectTemplateMapper.delete(id);
        inspectItemMapper.delete(id);
    }

    @Override
    public List<PollingTemplateInfoEntity> findAllName() {
        return inspectTemplateMapper.findAllName();
    }

}
